package com.xzq.imageviewertest;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {

    private List<String> urls;
    private int length;
    private Context context;
    private Activity activity;

    public ImageAdapter(List<String> urls, int width, Context context, Activity activity) {
        this.urls = urls;
        this.length = width;
        this.context = context;
        this.activity = activity;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_item_view, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {

        SimpleDraweeView imageView = new SimpleDraweeView(context);
        imageView.setLayoutParams(new LinearLayout.LayoutParams(length, length));

        GenericDraweeHierarchy hierarchy = GenericDraweeHierarchyBuilder.newInstance(context.getResources())
                .setRoundingParams(RoundingParams.fromCornersRadius(8))
                .build();
        imageView.setHierarchy(hierarchy);

        imageView.setImageURI(Uri.parse(urls.get(position)));
        holder.mainView.addView(imageView);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ImageGalleryActivity.class);
                Bundle bundle = new Bundle();
                ArrayList<CharSequence> imageUrls = new ArrayList<>();
                for (int i = 0; i < urls.size(); i++) {
                    imageUrls.add(urls.get(i));
                }
                bundle.putCharSequenceArrayList("imageList", imageUrls);
                bundle.putInt("position", position);
                intent.putExtras(bundle);
                context.startActivity(intent);
                activity.overridePendingTransition(R.anim.fade_out, R.anim.fade_in);
            }
        });
    }

    @Override
    public int getItemCount() {
        return urls.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        LinearLayout mainView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mainView = itemView.findViewById(R.id.item_main_view);
        }
    }

}
